var headerNavStatus = true;
mui.init();
(function($) {
	//页面a标签的点击跳转事件
	mui('body').on('tap', 'a', function (e) {
		if (!this.classList.contains('mui-disabled')) {
		mui.openWindow({
		url: this.getAttribute('href'),
		id: 'info'
		});
		}
		else {
		mui.preventDefault(e);
		mui.stopPropagation(e);
	}
	});
})
mui('body').on('tap','.editAccess',function(){
	window.location.href="/maidian/item/addOrModifyItem.htm?mallItemId="+$(this).attr('mallId');
})
mui('body').on('tap','.edit',function(){
	window.location.href="/maidian/item/addOrModifyItem.htm?mallItemId="+$(this).attr('mallId');
})
mui('body').on('tap','#menu',function(){
	if(headerNavStatus){
		$('#topPopover').fadeIn();
		headerNavStatus = false;
	}else{
		$('#topPopover').fadeOut();
		headerNavStatus = true;
	}
})
//删除商品
$('.delete').on('click',function(){
	modifyItemStatus(-8);
})
//上架商品
$('.shangjia').on('click',function(){
	modifyItemStatus(0);
})
//下架商品
$('.xiajia').on('click',function(){
	modifyItemStatus(-1);
})

function modifyItemStatus(status){
	var mallItemId = $("#mallItemId").val();
	$.ajax({
		type:"POST",
		url : "/maidian/item/modifyItemStatus.json",
		dataType : "json",
		data : {
			"mallItemId" :mallItemId,
			"status" :status
		},
		success : function(data) {
			console.log(data);
			if(data["code"]=="-200"){
				$('#errorInfo').html('连接错误请重试');
				$('.mui-active').show();
				$('.successTip').show();
			}else if(data["code"] == "200") {
				$('#errorInfo').html('修改成功');
				$('.mui-active').show();
				$('.successTip').show();
			}
		}
	})
}
function initStatus(){
	var status = $('#carStatus').val();
	if(status == 0){
		//已收藏
		$('.footer').removeClass('addShopTo').addClass('shopCartIn');
	}else{
		//未收藏
		$('.footer').addClass('addShopTo').removeClass('shopCartIn');
	}
}
initStatus();

//加入购物车
$('.addShopTo').on('click',function(e){
	e.preventDefault();
	var mallItemId = $(this).attr('mallItemId');//shopId to ajax post
    $(this).find('img').attr('src',$(this).find('img').attr('data-src'));
    $.ajax({
    	type:"POST",
		url : "/maidian/distributor/itemCollection.json",
		dataType : "json",
		data : {
			"mallItemId" :mallItemId
		},
		success : function(data) {
			$('.footer').removeClass('addShopTo').addClass('shopCartIn');
			$('#errorInfo').html("是否跳转至购物车支付");
			$('.mui-active').show();
			$('#errorTip').show();
		}
    });
})

$('.addShopIcon').on('click',function(e){
	e.preventDefault();
})

//友情提示
$('.queren').on('click',function(){
	$('#topPopover').fadeOut();
})
$(document).on('click','.shopCartIn',function(){
	mui.toast('已加入购物车');
})

//友情提示弹框
function tip(error){
	$('#errorInfo').html(error);
	$('.mui-active').show();
	$('#errorTip').show();
}
//友情提示确认
$('.queren').on('click',function(){
	window.location.href="/maidian/distributor/shoppingCart.htm";
})
//友情提示取消
$('.cancel').on('click',function(){
	$('.mui-active').hide();
	$('#errorTip').hide();
})
$('.successBtn').on('click',function(){
	$('.mui-active').hide();
	$('.successTip').hide();
	$('#topPopover').hide();
	headerNavStatus = true;
})

